home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 12781 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!psinntp!usenet
  2. From: grantp@usa.pipeline.com(Pete Grant)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C++ questions - please help
  5. Date: 21 Mar 1996 17:04:43 GMT
  6. Organization: Kalevi, Inc.
  7. Message-ID: <4is27b$cs5@news1.h1.usa.pipeline.com>
  8. References: <4irp34$b6q@GRAPEVINE.LCS.MIT.EDU>
  9. NNTP-Posting-Host: 38.8.53.2
  10. X-PipeUser: grantp
  11. X-PipeHub: usa.pipeline.com
  12. X-PipeGCOS: (Pete Grant)
  13. X-Newsreader: Pipeline v3.5.0
  14.  
  15. On Mar 21, 1996 14:28:52 in article <C++ questions - please help>,
  16. 'frankkim@antoniades.lcs.mit.edu (Frank Kim)' wrote: 
  17.  
  18.  
  19. >Hi, 
  20. >I was looking through some code and some of the stuff just stumped me. 
  21. >Question #1: 
  22. >There is a class defined like this... 
  23. >class faxRmApp : public FaxClient { 
  24. >private: 
  25. >fxStr       appName;                // for error messages 
  26. >fxStrArray  jobids; 
  27. >fxStr       request; 
  28. >void usage(); 
  29. >void printError(const char* fmt, ...); 
  30. >void printWarning(const char* fmt, ...); 
  31. >public: 
  32. >faxRmApp(); 
  33. >~faxRmApp(); 
  34. >void initialize(int argc, char** argv); 
  35. >void open(); 
  36. >void recvConf(const char* cmd, const char* tag); 
  37. >void recvEof(); 
  38. >void recvError(const int err); 
  39. >}; 
  40. >and its constructor looks like this: 
  41. >faxRmApp::faxRmApp() : request("remove") {} 
  42. >I don't understand why the request("remove") initialization is outside 
  43. >the brackets. 
  44. Standard initialization syntax.  The initializations between the ":" and 
  45. "{" are performed first.  Typically, this syntax is for initializing 
  46. base classes and reference member variables, but works just as well 
  47. for ordinary data members.  In fact, if the data member is an object, 
  48. and it's not included in the initializer list, it will be constructed 
  49. with its default constructor and then reinitialized again in the body 
  50. of the derived object's constructor.  This could result in significant 
  51. inefficiencies in certain cases.  I tend to initialize everything in 
  52. the initializer part, often ending up with an empty constructor body. 
  53.  
  54. >The next questions are even dumber. :) 
  55. >Question #2 
  56. >Why does the scope of the external declaration not extend beyond the 
  57. >inner block? 
  58. >{ 
  59. >{ 
  60. >extern E; 
  61. >E = 0; 
  62. >} 
  63. >E = 1; 
  64. >} 
  65. Don't confuse scope and duration (lifetime) of an aobject.  The  
  66. scope (ability to access) is restricted to the scope within which  
  67. declared.  The object (integer variable E in this case) exists 
  68. throughout the lifetime of the entire program, but accessibility 
  69. is limited to the blocks that contain the extern declaration --  
  70. and the block within which it's defined. 
  71. >Question #3 
  72. >Is there any difference between these two declarations? 
  73. >char *s1 = "hello"; 
  74. >char s2[] = "hello"; 
  75. Yes.  You can change s1 to point to some other string later on; 
  76. s2, however, will always be the address of a string, at least 
  77. for its duration.   
  78.  
  79. Another thing, the first form creates a string 
  80. in permament memory containing "hello", and s1 is a pointer variable 
  81. whose initial value is the address of this "hello" string.  This  
  82. "hello" string will continue to exist even after s1 is destroyed. 
  83. In the second form, the string is allocated at the place where 
  84. s2 is defined and ceases to exist when s2 is disposed of.  Often, 
  85. s2 is stack-allocated and disappears when the function exits. 
  86.  
  87. -- 
  88. Pete Grant 
  89. Kalevi, Inc. 
  90. Software Engineering & development
  91.